/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.refinement;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.RDF;
import edu.yonsei.iwb.sdt.datastructure.TargetInstance;
import java.util.Vector;
import edu.yonsei.iwb.sdt.ontology.PropertyArc;

/**
 *
 * @author JDK
 */
public class QualificationRefinement extends Refinement {

    private String name = new String();
    private String corename = new String();
    private ExpandingHistory eh;
    private String conditionr;

    public QualificationRefinement(String name, String corename, ExpandingHistory eh) {
        this.name = name;
        this.eh = eh;
        this.corename = corename;
        super.refinementType = RefinementType.QUALIFICATION;
    }

    public QualificationRefinement(QualificationRefinement qr) {
        this.name = qr.getName();
        this.eh = qr.getExpandingHistory();
        this.corename = qr.corename;
        this.conditionr = qr.getConditionResource();
        super.refinementType = RefinementType.QUALIFICATION;
    }

//    public QualificationRefinement(int standard, String prefix, Property r) {
//        this.name = standard + prefix + r.getLocalName().toString();
//        
//    }
    public QualificationRefinement(String prefix, ExpandingHistory eh, String classname) {
        this.eh = eh;
        Vector<String> quantifiers = this.eh.getQuantifierSet();
        Vector<PropertyArc> propertySet = this.eh.getPropertySet();
//        this.standardClass = r;
        
        PropertyArc firstpa = propertySet.firstElement();
        String firstp = firstpa.getProperty();
        String firstq = quantifiers.firstElement();
        if (firstpa.getDirection()) {                
            this.name = prefix+ " "+ firstq + " " + firstp+ ".(" + classname + ")";
        } else {
            this.name = prefix + " " + firstq + " " + firstp+ "￣.(" + classname+ ")";
        }
                
        this.corename = classname;
        this.conditionr = classname;
//        this.corename = quantifiers.firstElement() + propertySet.firstElement().getLocalName() +".("+ r.getLocalName().toString() + ")";
        for (int i = 1; i < propertySet.size(); i++) {
            PropertyArc pa = propertySet.get(i);
            String p = pa.getProperty();
            String q = quantifiers.get(i);
            if (pa.getDirection()) {                
                this.name = q + " " + p+ ".(" + this.name + ")";
            } else {
                this.name = q + " " + p+ "￣.(" + this.name + ")";
            }
        }

//        this.name = prefix + this.name;
        this.cc = new ConstraintConstructor(classname);
        super.refinementType = RefinementType.QUALIFICATION;
    }

    @Override
    public ExpandingHistory getExpandingHistory() {
        return this.eh;
    }

    @Override
    public void generateRefinementName() {
        super.refinement_name = this.name;
    }

    @Override
    public Refinement generateUpdatedRefinement(Vector<TargetInstance> instanceSet) {
        Refinement ref = new QualificationRefinement(this);
        ref.setBranchingType(this.branching_type);
        ref.setConstraintConstructor(this.cc);
        Vector<TargetInstance> oldinsSet = this.getInstanceSet();
        for (int i = 0; i < instanceSet.size(); i++) {
            TargetInstance ti = instanceSet.get(i);
            if (oldinsSet.contains(ti)) {
                ref.setInstanceSet(ti);
            }
        }
//        private boolean refinement_type = true;
//        protected String refinement_name;
//        private Vector<Resource> instanceSet;
        return ref;
    }

    @Override
    public String getCoreName() {
        return this.corename;
    }

    public String getName() {
        return name;
    }

    public String getConditionResource() {
        return conditionr;
    }

    @Override
    public Vector<Resource> getConstraintMatcherInstance(Model model) {
        Vector<Resource> matchedSet = new Vector();

        ResIterator res = model.listResourcesWithProperty(RDF.type, this.conditionr);
        while (res.hasNext()) {
            matchedSet.add(res.nextResource());
        }
//        throw new UnsupportedOperationException("Not supported yet.");
        return matchedSet;
    }
}
